import request from '@/utils/request';
import { ResponseData, SUCCESS_CODE } from '@/utils/types.d';
import { ExceptionOrderDetail, ExceptionOrderTableItem } from './types.d';

export async function queryExceptionOrder(
  params: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: ExceptionOrderTableItem[];
}> {
  const res: ResponseData<{
    total: number;
    data: ExceptionOrderTableItem[];
  }> = await request('/api/order/v1.0.0/abnormal/list', {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res?.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function getExceptionOrderDetail(id: string): Promise<{
  success: boolean;
  message: string;
  data: ExceptionOrderDetail;
}> {
  const res: ResponseData<ExceptionOrderDetail> = await request(
    '/api/order/v1.0.0/abnormal/detail',
    {
      params: {
        id,
      },
    },
  );
  if (res?.code === SUCCESS_CODE) return { success: true, message: '获取成功', data: res.data };
  return { success: false, message: res?.message || '获取失败', data: res.data };
}

export async function checkOrder(
  id: string,
  params: {
    correctType: number;
    remark: string;
  },
) {
  const res = await request('/api/order/v1.0.0/abnormal/fix', {
    method: 'POST',
    data: {
      id,
      correctType: params.correctType,
      remark: params.remark,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '操作成功',
    };
  return {
    data: res.data,
    success: false,
    message: res?.message || '操作失败',
  };
}
